/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.message;

import hextd.message.MessageFactory.NetworkMessage;
import hextd.server.Channel;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageChannelList implements Message {

	public class ChannelInfo {

		public String name;
		public int id;
		public boolean open;
		public boolean started;
		public int playerCount;

		@Override
		public String toString() {
			return name;
		}
	}
	public NetworkMessage type = NetworkMessage.ChannelList;
	public static char separator = ';';
	public static char separator2 = ',';
	public static String RegExp = "CLIST (" + separator + "([^" + separator + "]+" + separator + ")+)";
	public static Pattern MessagePattern = Pattern.compile(MessageChannelList.RegExp);
	//
	private String message;
	private HashMap<Integer, ChannelInfo> channels;

	public static String formatMessage(Collection<Channel> channels) {
		String message = "CLIST " + separator + "";
		for (Channel c : channels) {
			try {
				message += "" + c.getId() +
						separator2 + URLEncoder.encode(c.getName(), "UTF-8") +
						separator2 + Boolean.toString(c.isOpen()) +
						separator2 + Boolean.toString(c.isGameStarted()) +
						separator2 + c.getPlayerCount() +
						separator + "";
			} catch (UnsupportedEncodingException ex) {
				Logger.getLogger(MessageChannelList.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return message;
	}

	public MessageChannelList() {
	}

	private MessageChannelList(String message) {
		this.message = message;
		Matcher matcher = MessageChannelList.MessagePattern.matcher(message);
		if (matcher.matches()) {
			this.channels = new HashMap<Integer, ChannelInfo>();
			String names = matcher.group(1);
			names = names.substring(1, names.length() - 1);
			String[] channelsArray = names.split("" + separator);
			for (int i = 0; i < channelsArray.length; i++) {
				String[] channelArray = channelsArray[i].split("" + separator2);
				try {
					ChannelInfo info = new ChannelInfo();
					info.id = new Integer(channelArray[0]);
					info.name = URLDecoder.decode(channelArray[1], "UTF-8");
					info.open = Boolean.parseBoolean(channelArray[2]);
					info.started = Boolean.parseBoolean(channelArray[3]);
					info.playerCount = new Integer(channelArray[4]);
					channels.put(info.id, info);
				} catch (NumberFormatException e) {
					System.err.println("MessageChannelList: invalid number in: " + channelsArray[i] + " from: " + message);
				} catch (UnsupportedEncodingException ex) {
					Logger.getLogger(MessageChannelList.class.getName()).log(Level.SEVERE, null, ex);
				}

			}

		}
	}

	public HashMap<Integer, ChannelInfo> getChannels() {
		return channels;
	}

	@Override
	public String getMessage() {
		return this.message;
	}

	@Override
	public Message initFromString(String message) {
		return new MessageChannelList(message);
	}

	@Override
	public int getSenderId() {
		return -1;
	}

	@Override
	public void setSenderId(int id) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public Pattern getPattern() {
		return MessageChannelList.MessagePattern;
	}

	@Override
	public NetworkMessage getType() {
		return this.type;
	}
}
